package org.lex.filter;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Arrays;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.lex.utils.text.Splitter;


public class KeywordFilterPane extends JPanel {
	private JComponent[][] allComponents = null;
	private boolean[] paneVisible = null;
	private JLabel includeAnyLabel = null;
	private JTextField includeAnyTextField = null;
	private JLabel includeAllLabel = null;
	private JTextField includeAllTextField = null;
	private JLabel excludeAnyLabel = null;
	private JTextField excludeAnyTextField = null;
	private JLabel excludeAllLabel = null;
	private JTextField excludeAllTextField = null;

	public KeywordFilterPane() {
		super();
		initialize();
		// public static enum Type
		// {
		// INCLUDE_ANY, INCLUDE_ALL, EXCLUDE_ANY, EXCLUDE_ALL
		// };
		// ע�������˳����KeywordQuery.Type��˳�������һ�µ�
		this.allComponents = new JComponent[][] { { includeAnyLabel, includeAnyTextField }, { includeAllLabel, includeAllTextField },
				{ excludeAnyLabel, excludeAnyTextField }, { excludeAllLabel, excludeAllTextField } };
		this.paneVisible = new boolean[KeywordFilter.Type.values().length];
		Arrays.fill(paneVisible, true);
	}

	public KeywordFilter getKeywordFilter() {
		return this.getKeywordQuery(new KeywordFilter());
	}

	public KeywordFilter getKeywordQuery(KeywordFilter query) {
		KeywordFilter.Type[] types = KeywordFilter.Type.values();
		for (KeywordFilter.Type type : types) {
			String text = this.getText(type);
			if (0 == text.length())
				continue;
			String[] keys = Splitter.BlankSpaceSplitter.split(text);
			query.add(type, keys);
		}
		return query;
	}

	public String getText(KeywordFilter.Type type) {
		return ((JTextField) allComponents[type.ordinal()][1]).getText().trim();
	}

	// ===================================================
	public void setVisible(KeywordFilter.Type type, boolean value) {
		JComponent[] sub = this.allComponents[type.ordinal()];
		for (JComponent c : sub)
			c.setVisible(value);
		this.paneVisible[type.ordinal()] = value;
	}

	public boolean isVisible(KeywordFilter.Type type) {
		return this.paneVisible[type.ordinal()];
	}

	// =====================
	private void initialize() {
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints7.gridy = 3;
		gridBagConstraints7.weightx = 1.0;
		gridBagConstraints7.anchor = GridBagConstraints.WEST;
		gridBagConstraints7.insets = new Insets(0, 5, 0, 5);
		gridBagConstraints7.gridx = 1;
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.gridx = 0;
		gridBagConstraints6.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints6.anchor = GridBagConstraints.WEST;
		gridBagConstraints6.gridy = 3;
		excludeAllLabel = new JLabel();
		excludeAllLabel.setText("Exclude All");
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints5.gridy = 2;
		gridBagConstraints5.weightx = 1.0;
		gridBagConstraints5.anchor = GridBagConstraints.WEST;
		gridBagConstraints5.insets = new Insets(0, 5, 3, 5);
		gridBagConstraints5.gridx = 1;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 0;
		gridBagConstraints4.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints4.anchor = GridBagConstraints.WEST;
		gridBagConstraints4.gridy = 2;
		excludeAnyLabel = new JLabel();
		excludeAnyLabel.setText("Exclude Any");
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints3.gridy = 1;
		gridBagConstraints3.weightx = 1.0;
		gridBagConstraints3.anchor = GridBagConstraints.WEST;
		gridBagConstraints3.insets = new Insets(0, 5, 3, 5);
		gridBagConstraints3.gridx = 1;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints2.anchor = GridBagConstraints.WEST;
		gridBagConstraints2.gridy = 1;
		includeAllLabel = new JLabel();
		includeAllLabel.setText("Include All");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.gridy = 0;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.anchor = GridBagConstraints.WEST;
		gridBagConstraints1.insets = new Insets(0, 5, 3, 5);
		gridBagConstraints1.gridx = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.gridy = 0;
		includeAnyLabel = new JLabel();
		includeAnyLabel.setText("Include Any");
		this.setLayout(new GridBagLayout());
		this.setSize(300, 200);
		this.add(includeAnyLabel, gridBagConstraints);
		this.add(getIncludeAnyTextField(), gridBagConstraints1);
		this.add(includeAllLabel, gridBagConstraints2);
		this.add(getIncludeAllTextField(), gridBagConstraints3);
		this.add(excludeAnyLabel, gridBagConstraints4);
		this.add(getExcludeAnyTextField(), gridBagConstraints5);
		this.add(excludeAllLabel, gridBagConstraints6);
		this.add(getExcludeAllTextField(), gridBagConstraints7);
	}

	private JTextField getIncludeAnyTextField() {
		if (includeAnyTextField == null) {
			includeAnyTextField = new JTextField();
		}
		return includeAnyTextField;
	}

	private JTextField getIncludeAllTextField() {
		if (includeAllTextField == null) {
			includeAllTextField = new JTextField();
		}
		return includeAllTextField;
	}

	private JTextField getExcludeAnyTextField() {
		if (excludeAnyTextField == null) {
			excludeAnyTextField = new JTextField();
		}
		return excludeAnyTextField;
	}

	private JTextField getExcludeAllTextField() {
		if (excludeAllTextField == null) {
			excludeAllTextField = new JTextField();
		}
		return excludeAllTextField;
	}
}
